class Solution {
public:
    int minDepth(TreeNode* root) { 
        queue<TreeNode*>q;
        if(root)q.push(root);
        int ret=0;
        while(q.size())
        {
            ret++;
            int sz=q.size();
            while(sz--)
            {
                auto root=q.front();
                q.pop();
                if(!root->left&&!root->right)return ret;
                if(root->left)q.push(root->left);
                if(root->right)q.push(root->right);
            }
        }
        return ret;
    }
};
